﻿/*
   @Description:个人中心写消息页面js
   @Author:ZB,YPL
   @Date:2014-04-10 
 */

var MessageSending = {};
MessageSending.init = function () {
    $("#imgSendInfo").click(function() {
        $.dialog({
            id: "dialogAddRecipient",
            title: "添加收件人",
            lock: true,
            background: "#808080",
            opacity: 0.87,
            content: '<div id="spanAddRecipient" style="height:25px;line-height:25px;">' + $("#divSendInfoModel").html() + '</div>',
            init: function() {
                EmployeePrompt.getIDAndCornetByInput('txtUserName', 'txtUserId', 'txtCornet', true, 'private');
            },
            cancel: true
        });
    });
    //发送信息按钮
    $('#btnSend').click(function () {
        var userId = $('#txtUserId').val();
        var content = $('#Content').val();
        if ($("#spanAddContacts").find("span").length <= 0) {
            $.OKDialogAgency('<div class="tc">没有选择任何联系人!</div>');
            return false;
        }
        if (!content || content.match(/^\s+$/) || content.match(/^\xA0+$/)) {
            $.OKDialogAgency('<div class="tc">消息内容不能为空!</div>', '提示', function () {
                $("#Content").focus();
            });
            return false;
        }
        var tempUserIds = "";
        $("#spanAddContacts").find("input[name=contactsKeyId]").each(function () {
            tempUserIds += ","+$(this).val();
        });
        tempUserIds = tempUserIds.substr(1);
        var reqData = {
            'ReceiverKeyId': tempUserIds,
            'Content': $('#Content').val()
        };
        $.AjaxAgency('/Center/batch-create-message', reqData, function (data) {
            if (data == 'ok') {
                $.OKDialogAgency('<div class="tc">发送成功！</div>', '提示', function () {
                    $('#txtUserName').val('');
                    $('#txtUserId').val('');
                    $('#Content').val('');
                    $("#spanAddContacts").html("");
                    $(".jq_remainderNumber .red").html("400");
                });
            } else {
                $.OKDialogAgency('<div class="tc">' + data + '</div>');
            }
        });
    });
};

//添加联系人
MessageSending.appendContactsClickEvent = function () {
    if ($('#txtUserId').val().length < 2) {
        $.OKDialogAgency('<div class="tc">请从智能提示中选择联系人!</div>');
        return false;
    }

    var contactsName = $("#txtUserName").val();
    var contactsKeyId = $("#txtUserId").val();
    var cornet = other.trim($("#txtCornet").val());
    var isRepeatContacts = false;
    //判断要添加的联系人是否重复
    $("#spanAddContacts").find("input[name=contactsKeyId]").each(function () {
        if ($(this).val() == contactsKeyId) {
            isRepeatContacts = true;
            return;
        }
    });
    if (isRepeatContacts) {
        $.OKDialogAgency('<div class="tc">不能重复添加联系人!</div>');
        return false;
    }

    //清空联系人信息与关联隐藏域
    $("#txtUserName").val("");
    $("#txtUserId").val("");
    $("#txtCornet").val('');
    var test = $('#spanAddContacts').html();
    if (cornet == null || cornet == "") {
        test += '<span class="l">' + contactsName + '<a class="mr10"><i class="fclose" onclick="MessageSending.DelContactsClickEvent(this);">×</i></a><input type="hidden" name="contactsName" value="' + contactsName + '" /><input type="hidden" name="contactsKeyId" value="' + contactsKeyId + '" /></span>';
    }
    else {
        test += '<span class="l">' + contactsName + "("+cornet+")"+'<a class="mr10"><i class="fclose" onclick="MessageSending.DelContactsClickEvent(this);">×</i></a><input type="hidden" name="contactsName" value="' + contactsName + '" /><input type="hidden" name="contactsKeyId" value="' + contactsKeyId + '" /></span>';
    }
    $('#spanAddContacts').html(test);
};

//删除联系人
MessageSending.DelContactsClickEvent = function (ele) {
    $(ele).parent().parent().remove();
};